package com.example.controller;

import com.example.common.Result;
import com.example.entity.Cart;
import com.example.service.CartService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 购物车(Cart)表控制层
 *
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;

    //信息列表
    @ResponseBody
    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "1") Integer pageSize,
                      HttpServletRequest request) {
        String key = request.getParameter("key");
        String memberidStr = request.getParameter("memberid");
        String goodsidStr = request.getParameter("goodsid");
        
        Integer memberid = memberidStr != null && !memberidStr.isEmpty() ? Integer.parseInt(memberidStr) : null;
        Integer goodsid = goodsidStr != null && !goodsidStr.isEmpty() ? Integer.parseInt(goodsidStr) : null;
        
        PageInfo<Cart> pageInfo = cartService.getCartList(pageNum, pageSize, key, memberid, goodsid);
        return Result.success(pageInfo);
    }
    
    //添加购物车信息
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Cart cart, HttpServletRequest request) {
        cartService.addCart(cart);
        return Result.success();
    }

    //编辑购物车信息
    @ResponseBody
    @RequestMapping("/edit")
    public Result edit(Cart cart, HttpServletRequest request) {
        cartService.updateCart(cart);
        return Result.success();
    }

    //删除信息
    @ResponseBody
    @RequestMapping("/del")
    public Result del(int id, HttpServletRequest request) {
        cartService.deleteCart(id);
        return Result.success();
    }
    

}

